﻿using Mal.Web.Security;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Configuration.Provider;
using Mal.Web.Security.Provider;
using System.Threading;
using System;
namespace Mal.Web.Security.UnitTest
{
    
    
    /// <summary>
    ///This is a test class for PermissionsTest and is intended
    ///to contain all PermissionsTest Unit Tests
    ///</summary>
    [TestClass()]
    public class PermissionsTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        public void Initialize()
        {
            Permissions.Provider = new SqlPermissionProvider("PermissionProviderUnitTest"
                , "Data Source=PC-BUREAU;Initial Catalog=MalLinqRoleProvider;Integrated Security=True");
        }
               

        /// <summary>
        ///A test for UserHasPermission
        ///</summary>
        [TestMethod()]
        public void UserHasPermissionTest()
        {
            string userName = string.Empty; // TODO: Initialize to an appropriate value
            string permissionName = string.Empty; // TODO: Initialize to an appropriate value
            bool expected = false; // TODO: Initialize to an appropriate value
            bool actual;

            try
            {
                actual = Permissions.UserHasPermission(permissionName);
                Assert.Fail("ProviderException should be throw");
            }
            catch (ProviderException exp)
            {

            }

            Initialize();            

            userName = "";
            permissionName = "";
            try
            {
                actual = Permissions.UserHasPermission(userName, permissionName);
                Assert.Fail("userName is null, ArgumentNullException should be throw");
            }
            catch (ArgumentNullException exp)
            {

            }

            userName = "";
            permissionName = "Perm1";
            try
            {
                actual = Permissions.UserHasPermission(userName, permissionName);
                Assert.Fail("userName is null, ArgumentNullException should be throw");
            }
            catch (ArgumentNullException exp)
            {

            }

            userName = "User1";
            permissionName = "";
            try
            {
                actual = Permissions.UserHasPermission(userName, permissionName);
                Assert.Fail("permissionName is null, ArgumentNullException should be throw");
            }
            catch (ArgumentNullException exp)
            {

            }

            userName = "User1";
            permissionName = "Perm1";           
            actual = Permissions.UserHasPermission(userName, permissionName);
            Assert.AreEqual(true, actual);

            userName = "User1";
            permissionName = "Perm2";
            actual = Permissions.UserHasPermission(userName, permissionName);
            Assert.AreEqual(true, actual);

            userName = "User1";
            permissionName = "Perm3";
            actual = Permissions.UserHasPermission(userName, permissionName);
            Assert.AreEqual(false, actual);

            userName = "User1";
            permissionName = "Perm4";
            actual = Permissions.UserHasPermission(userName, permissionName);
            Assert.AreEqual(false, actual);



            userName = "User2";
            permissionName = "Perm1";
            actual = Permissions.UserHasPermission(userName, permissionName);
            Assert.AreEqual(true, actual);

            userName = "User2";
            permissionName = "Perm2";
            actual = Permissions.UserHasPermission(userName, permissionName);
            Assert.AreEqual(true, actual);

            userName = "User2";
            permissionName = "Perm3";
            actual = Permissions.UserHasPermission(userName, permissionName);
            Assert.AreEqual(true, actual);

            userName = "User2";
            permissionName = "Perm4";
            actual = Permissions.UserHasPermission(userName, permissionName);
            Assert.AreEqual(false, actual);
           
        }
    }
}
